// packageI/taskPackage/taskPackageIndex/taskPackageIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    selected: {},
    isLoadMore: true,
    usable_name: '', //爱心值名称
    tabOptions: [{
      isLoadMore: true,
      page: 1,
      total_page: 0,
      list: [],
      url: 'plugin.task-package.frontend.bag.get-list',
      othderJson: {},
      name: '任务商城'
    }, {
      isLoadMore: true,
      page: 1,
      total_page: 0,
      list: [],
      url: 'plugin.task-package.frontend.record.get-list',
      othderJson: {
        status: 1
      },
      name: '运行中'
    }, {
      isLoadMore: true,
      page: 1,
      total_page: 0,
      list: [],
      url: 'plugin.task-package.frontend.record.get-list',
      othderJson: {
        status: 2
      },
      name: '已过期'
    }],
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF8924',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    });
    wx.setNavigationBarTitle({
      title: '普法任务',
    })
  },
  initData() {
    this.setData({
      active: 0,
      isLoadMore: true,
      tabOptions: [{
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: [],
        url: 'plugin.task-package.frontend.bag.get-list',
        othderJson: {},
        name: '任务商城'
      }, {
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: [],
        url: 'plugin.task-package.frontend.record.get-list',
        othderJson: {
          status: 1
        },
        name: '运行中'
      }, {
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: [],
        url: 'plugin.task-package.frontend.record.get-list',
        othderJson: {
          status: 2
        },
        name: '已过期'
      }]
    })
  },
  toMyTask() {
    wx.navigateTo({
      url: '/packageI/taskPackage/taskPackageMy/taskPackageMy',
    })
  },
  tapSelectChange(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      selected: item
    })
  },
  tapTask(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageI/taskPackage/task/task?id='+id,
    })
  },
  getData() {
    if (!app._isTextEmpty(this.data.tabOptions[this.data.active].list)) {
      this.setData({
        isLoadMore: this.data.tabOptions[this.data.active].isLoadMore
      })
      return;
    }

    let json = {
      ...this.data.tabOptions[this.data.active].othderJson,
      page: 1
    }
    app._getNetWork({
      url: app.getNetAddresss(this.data.tabOptions[this.data.active].url),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          this.data.tabOptions[this.data.active].list = data.data || [];
          this.data.tabOptions[this.data.active].total_page = data.last_page;
          this.data.tabOptions[this.data.active].isLoadMore = true;
          this.setData({
            tabOptions: this.data.tabOptions,
            isLoadMore: true,
            usable_name: data.usable_name?data.usable_name:'可用爱心值'
          })
          if (this.data.active == 0) {
            this.setData({
              selected: this.data.tabOptions[0].list[0]
            })
          }
          console.log(this.data.tabOptions, 'ddddddddd')
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.tabOptions[this.data.active].isLoadMore = false;
    this.setData({
      isLoadMore: false,
      tabOptions: this.data.tabOptions
    })
    if (this.data.tabOptions[this.data.active].page >= this.data.tabOptions[this.data.active].total_page) {
      return;
    }
    this.data.tabOptions[this.data.active].page = this.data.tabOptions[this.data.active].page + 1;
    this.setData({
      tabOptions: this.data.tabOptions
    })
    let page = this.data.tabOptions[this.data.active].page;
    let json = {
      ...this.data.tabOptions[this.data.active].othderJson,
      page
    };
    app._getNetWork({
      url: app.getNetAddresss(this.data.tabOptions[this.data.active].url),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          this.data.tabOptions[this.data.active].isLoadMore = true;
          this.data.tabOptions[this.data.active].list = this.data.tabOptions[this.data.active].list.concat(data.data);
          this.data.tabOptions[this.data.active].total_page = data.last_page;
          this.setData({
            isLoadMore: true,
            tabOptions: this.data.tabOptions
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  exchang() {
    if (!this.data.selected.id) {
      wx.showToast({
        title: '请选择需要兑换的任务',
      })
      return;
    }
    wx.showModal({
      title: '',
      content: `是否确定用${this.data.selected.love_amount}${this.data.usable_name}兑换该任务包`,
      complete: (resin) => {
        if (resin.confirm) {
          app._getNetWork({
            url: app.getNetAddresss("plugin.task-package.frontend.record.exchange-bag"),
            data: {
              id: this.data.selected.id
            },
            success: (resdata) => {
              var res = resdata.data;
              wx.showModal({
                content: res.msg,
                showCancel: false,
                complete: (resinfo) => {
                  if (resinfo.confirm) {
                    if (res.result == 1) {
                      this.initData();
                      this.getData();
                    }
                  }
                }
              })
            }
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoadMore){
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChange(e) {
    console.log(e,'ddddddddddddddddd')
    this.setData({
      active: e.detail.index
    })
    this.getData();
  }
})